import remove from 'lodash/remove'

let paramTypeList = [
  {paId: '00001', paName: 'db-MySQL', paAlias: 'MySQL库配置', isSys: 1, createTime: '2020-05-28'},
  {paId: '00002', paName: 'postCode-canton', paAlias: '广州市邮编信息', isSys: 0, createTime: '2020-05-27'}
]

let paramList = [
  {paId: '01001', paKey: '广州市', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 1},
  {paId: '01002', paKey: '荔湾区', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 2},
  {paId: '01003', paKey: '越秀区', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 3},
  {paId: '01004', paKey: '海珠区', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 4},
  {paId: '01005', paKey: '天河区', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 5},
  {paId: '01006', paKey: '白云区', paDesc: '', paValue: '510000', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 6},
  {paId: '01007', paKey: '黄埔区', paDesc: '', paValue: '510700', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 7},
  {paId: '01008', paKey: '番禺区', paDesc: '', paValue: '511400', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 8},
  {paId: '01009', paKey: '花都区', paDesc: '', paValue: '510800', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 9},
  {paId: '01010', paKey: '南沙区', paDesc: '', paValue: '511400', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 10},
  {paId: '01011', paKey: '增城区', paDesc: '', paValue: '511300', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 11},
  {paId: '01012', paKey: '从化区', paDesc: '', paValue: '510900', typeId: '00002', isSys: 0, createTime: '2020-05-27', order: 12},
  {paId: '02001', paKey: 'dbName', paDesc: '数据库名', paValue: 'testDB', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 1},
  {paId: '02002', paKey: 'driverClass', paDesc: '连接驱动', paValue: 'com.mysql.cj.jdbc.Driver', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 2},
  {paId: '02003', paKey: 'url', paDesc: '连接字符串', paValue: 'jdbc:mysql://192.168.36.129:3306/testDB', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 3},
  {paId: '02004', paKey: 'username', paDesc: '连接用户', paValue: 'root', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 4},
  {paId: '02005', paKey: 'password', paDesc: '连接密码', paValue: 'admin', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 5},
  {paId: '02006', paKey: 'character', paDesc: '字符集', paValue: 'UTF-8', typeId: '00001', isSys: 1, createTime: '2020-05-28', order: 6},
  {paId: '02007', paKey: 'test1', paDesc: '', paValue: 'test1', typeId: '00001', isSys: 0, createTime: '2020-05-28', order: 7},
  {paId: '02008', paKey: 'test2', paDesc: '', paValue: 'test2', typeId: '00001', isSys: 0, createTime: '2020-05-28', order: 8}
]

export default {
  'post|/sys/param/delBatch': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const data = JSON.parse(option.body)
    remove(paramList, function (item) {
      return data.ids.indexOf(item.paId) > -1
    })
    ret.success = true
    ret.msg = '删除参数成功！'
    return ret
  },
  'post|/sys/param/del': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const id = JSON.parse(option.body)
    remove(paramList, function (item) {
      return item.id === id
    })
    ret.success = true
    ret.msg = '删除参数成功！'
    return ret
  },
  'post|/sys/param/type/del': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const id = JSON.parse(option.body)
    const hasChildren = paramList.some(item => {
      return item.typeId === id
    })
    if (!hasChildren) {
      remove(paramTypeList, function (item) {
        return item.id === id
      })
      ret.success = true
      ret.msg = '删除参数类型成功！'
    } else {
      ret.msg = '删除参数类型失败，该参数类型下存在参数数据，请先删除！'
    }
    return ret
  },
  'post|/sys/param/add': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const param = JSON.parse(option.body)
    const exist = paramList.some(item => {
      return item.key === param.key && item.typeId === param.typeId
    })
    if (!exist) {
      const newObj = {
        id: Math.floor((Math.random() * 100) + 1) + '',
        key: param.key,
        desc: param.desc,
        value: param.value,
        typeId: param.typeId,
        isSys: 0,
        crtTime: '2020-05-28',
        order: param.order
      }
      paramList.push(newObj)
      ret.success = true
      ret.msg = '新增参数成功！'
    } else {
      ret.msg = '新增参数失败，该参数已存在！'
    }
    return ret
  },
  'post|/sys/param/type/add': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const paramType = JSON.parse(option.body)
    const exist = paramTypeList.some(item => {
      return item.name === paramType.name
    })
    if (!exist) {
      const newObj = {
        id: Math.floor((Math.random() * 100) + 1) + '',
        name: paramType.name,
        alias: paramType.alias,
        isSys: 0,
        crtTime: '2020-05-27'
      }
      paramTypeList.push(newObj)
      ret.success = true
      ret.msg = '新增参数类型成功！'
    } else {
      ret.msg = '新增参数类型失败，该参数类型已存在！'
    }
    return ret
  },
  'post|/sys/param/edit': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const param = JSON.parse(option.body)
    let isUpdate = paramList.some(item => {
      if (item.id === param.id) {
        item.key = param.key
        item.desc = param.desc
        item.value = param.value
        item.order = param.order
        return true
      }
    })
    if (isUpdate) {
      ret.success = true
      ret.msg = '修改参数成功！'
    } else {
      ret.msg = '修改参数失败,找不到此Id！'
    }
    return ret
  },
  'post|/sys/param/type/edit': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const paramType = JSON.parse(option.body)
    let isUpdate = paramTypeList.some(item => {
      if (item.id === paramType.id) {
        item.name = paramType.name
        item.alias = paramType.alias
        return true
      }
    })
    if (isUpdate) {
      ret.success = true
      ret.msg = '修改参数类型成功！'
    } else {
      ret.msg = '修改参数类型失败,找不到此Id！'
    }
    return ret
  },
  'get|/sys/param/info': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const url = option.url
    const id = url.split('/')[url.split('/').length - 1]
    let obj = paramList.find((item) => {
      return item.id === id
    })
    if (obj) {
      ret.success = true
      ret.data = obj
    } else {
      ret.msg = '找不到参数Id为[' + id + ']的数据'
    }
    return ret
  },
  'get|/sys/param/type/info': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const url = option.url
    const typeId = url.split('/')[url.split('/').length - 1]
    let obj = paramTypeList.find((item) => {
      return item.id === typeId
    })
    if (obj) {
      ret.success = true
      ret.data = obj
    } else {
      ret.msg = '找不到参数类型Id为[' + typeId + ']的数据'
    }
    return ret
  },
  'get|/sys/param/type/list': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const params = url.split('?')[url.split('?').length - 1]
    const data = qs.parse(params)
    const name = data.name
    const newList = []
    // mock模拟数据不做分页
    paramTypeList.forEach((item) => {
      if (name !== '') {
        if (item.name.indexOf(name) > -1 || item.alias.indexOf(name) > -1) {
          newList.push(item)
          return true
        }
      } else {
        newList.push(item)
      }
    })
    ret.data = {
      totalCount: paramTypeList.length - 1,
      dataList: newList
    }
    return ret
  },
  'post|/sys/param/list': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const params = JSON.parse(option.body)
    const keyword = params.keyword
    const typeId = params.id
    const list = paramList.filter((item) => {
      return item.typeId === typeId
    })
    if (keyword !== '') {
      const newList = list.filter((item) => {
        return (item.key.indexOf(keyword) > -1 || item.desc.indexOf(keyword) > -1)
      })
      ret.success = true
      ret.data = {
        totalCount: list.length,
        dataList: newList
      }
    } else {
      ret.success = true
      ret.data = {
        totalCount: list.length,
        dataList: list
      }
    }
    return ret
  }
}
